package app.system.controll;

import app.system.entity.SysMusic;
import app.system.entity.SysMusicLike;
import app.system.entity.SysUser;
import app.system.service.SysMusicService;
import app.utils.Result.AjaxResult;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

@RestController
@RequestMapping("music")
public class MusicControll {

    @Autowired
    private SysMusicService musicService;

    @RequiresPermissions("register")
    @PostMapping("musicUpload")
    public AjaxResult musicUpload(HttpServletRequest request){
        SysUser userSys = (SysUser) SecurityUtils.getSubject().getPrincipal();
        System.out.println(JSONObject.toJSONString(userSys));
        MultipartHttpServletRequest servletRequest = (MultipartHttpServletRequest) request;
        Map<String,MultipartFile> multipartFileMap = servletRequest.getFileMap();
        //取出文件
        MultipartFile file = multipartFileMap.get("musicFile");
        //取出文件预览图
//        MultipartFile photo = multipartFileMap.get("music_photourl");
        String photoName = file.getOriginalFilename();
        System.out.println(photoName);
        //取出其余数据
        Map<String,String> parmMap=new HashMap<String,String>();
        Enumeration<String> a = request.getParameterNames();
        String parm=null;
        String val="";
        while(a.hasMoreElements()){
            //参数名
            parm=a.nextElement();
            //值
            val=request.getParameter(parm);
            parmMap.put(parm, val);
        }
        if (Objects.isNull(file)){
            return AjaxResult.error(-1,"文件上传失败，请重新选择文件");
        }
        return musicService.musicUpload(file,parmMap,userSys);
    }

    @GetMapping("musicDown")
    public AjaxResult musicDown(@RequestParam(name="music_id") String music_id,HttpServletResponse response){
        SysMusic sysMusic = new SysMusic();
        sysMusic.setMusic_id(music_id);
        return musicService.musicDownLoad(response,sysMusic);

    }

//    @RequiresPermissions("register")
    @GetMapping("musicDownload")
    public AjaxResult musicDownload(@RequestParam(name="music_id") String music_id, HttpServletResponse response){
        System.out.println(music_id);
        SysMusic sysMusic = new SysMusic();
        sysMusic.setMusic_id(music_id);
        return musicService.musicDownLoad(response,sysMusic);
    }

    @GetMapping("getMyMusic")
    public AjaxResult getMyMusic(@RequestParam(value = "pageNum", defaultValue = "1" ,required = false) int pageNum,
                                 @RequestParam(value = "pageSize", defaultValue = "12",required = false) int pageSize){

        SysMusic sysMusic = new SysMusic();
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        sysMusic.setMusic_uploadauthorid(sysUser.getUser_id());
        PageHelper.startPage(pageNum,pageSize);
        PageInfo<SysMusic> musicPageInfo = new PageInfo<SysMusic>(musicService.selectMusicByTage(sysMusic));
        if (musicPageInfo.getList().size()<1){
            return AjaxResult.success("您还没有上传音乐哦!");
        }else {
            return AjaxResult.success(musicPageInfo);
        }
    }

    @GetMapping("getMyMusicBytype")
    public AjaxResult getMyMusicBytype(@RequestParam(value = "pageNum", defaultValue = "1" ,required = false) int pageNum,
                                       @RequestParam(value = "pageSize", defaultValue = "25",required = false) int pageSize,
                                       @RequestParam(value = "type") String type){
        SysMusic sysMusic = new SysMusic();
        sysMusic.setMusic_type(type);
        PageHelper.startPage(pageNum,pageSize);
        PageInfo<SysMusic> musicPageInfo = new PageInfo<SysMusic>(musicService.selectMusicByTage(sysMusic));
        if (musicPageInfo.getList().size()<1){
            return AjaxResult.success("此类音乐为空");
        }else {
            return AjaxResult.success(musicPageInfo);
        }
    }

    @PostMapping("deleteMusic")
    public AjaxResult deleteMusic(@RequestBody SysMusic sysMusic){
        int de = musicService.deleteMusic(sysMusic);
        if (de<0){
            return AjaxResult.error("删除音乐失败");
        }else {
            return AjaxResult.success("删除成功");
        }
    }

    @PostMapping("getMusicListByType")
    public AjaxResult getMusicListByType(@RequestBody SysMusic sysMusic){
        List<SysMusic> sysMusicList = musicService.selectMusicByTage(sysMusic);
        return AjaxResult.success(sysMusicList);
    }

    @PostMapping("musicSearch")
    public AjaxResult musicSearch(@RequestBody SysMusic sysMusic){
        return AjaxResult.success(musicService.selectMusicByLike(sysMusic.getMusic_name()));
    }

}
